function modalBoxInit( ) {
    
    //$('#modalBoxCancel').click( closeModalBox );
    $(document).bind('keydown', modalBoxKeyPress );

}

function closeModalBox() {
    $('#modalBoxTitle').text();   
    $('#modalBoxForm').empty();
    $('#modalBoxContainer').hide();
}

function modalBoxKeyPress( e ) {

    var isModalBoxVisible = $('#modalBoxContainer').is(':visible');

    if(e.keyCode==27 && isModalBoxVisible ) { closeModalBox(); }
    //if(e.keyCode==13 && isModalBoxVisible ) { $('#modalBoxSubmit').click(); }
    if(e.keyCode==13 && isModalBoxVisible ) { $('#modalBoxForm input[type="submit"]').submit(); }
    
}

function getModalContent( p ) {

    $.ajax( { url: p.data.contentUrl,
              type: "POST",
              data: { },
              async: false,
              success: showModalDialogue } );

    $('#modalBoxForm input[type="submit"]').bind( 'click', trapSubmit )
                                           .bind( 'click', p.data.success.args, p.data.success.func )
                                           .bind( 'click', closeModalBox );
}

function trapSubmit() {

    var form = $('#modalBoxForm form');
    var qs = form.serialize();
    
    $.ajax( { url: form.attr('action') + '?' + qs,
              type: "POST",
              data: { },
              async: true } );
}

function showModalForm() { $('#modalBoxForm').show('slow'); }

function showModalDialogue( p ) {

    var p = eval( "(" + p + ")" );

    $('#modalBoxForm').append( p.form );

    $('#modalBoxTitle').text( p.title );

    $('#modalBoxContainer').show('slow', showModalForm );

    var contentContainer = $('#modalBoxContent');
    var contentHeight = contentContainer.height();
    var contentWidth = contentContainer.width();
    var myDocument = $(document);

    contentContainer.css( { top: ( ( myDocument.height() - contentHeight ) / 2 ),
                            left: ( ( myDocument.width() - contentWidth ) / 2 ) - ( contentWidth / 2 ) } );

    $( $('#modalBoxContainer input')[0] ).focus();
}

function showModalDialogue2( p ) {

    var p = eval( "(" + p + ")" );

    for( var i = 0, ii = p.data.fields.length; i < ii; i++ ) {

        $('#modalBoxForm').append(
            makeEl('div').attr( { 'class': 'padding10' } ).append(
                makeEl('span').attr( { 'class': 'padding10' } ).text( p.data.fields[i].text + " : " ),
                makeEl('input').attr( { 'name': p.data.fields[i].name, 'type': 'text', 'class': '' } ) ) );
    }

    $('#modalBoxTitle').text( p.data.title );

    $('#modalBoxContainer').show('slow');

    var contentContainer = $('#modalBoxContent');
    var contentHeight = contentContainer.height();
    var contentWidth = contentContainer.width();
    var myDocument = $(document);

    contentContainer.css( { top: ( ( myDocument.height() - contentHeight ) / 2 ),
                            left: ( ( myDocument.width() - contentWidth ) / 2 ) - ( contentWidth / 2 ) } );

    $( $('#modalBoxContainer input')[0] ).focus();

    $('#modalBoxSubmit').unbind('click').bind( 'click', { requestUrl: p.data.submitUrl,
                                                          jsSuccess: p.data.jsSuccess }, submitModalBox )
                                        .bind( 'click', { }, closeModalBox );
}

function submitModalBox( p ) {

    var data = gatherModalBoxInputData();

    $.ajax( { url: p.data.requestUrl,
              type: "POST",
              data: data,
              success: triggerEvent } );
}

function triggerEvent( p ) { var p = eval( "(" + p + ")" ); $(document).trigger( p.event, p.data ); }

function gatherModalBoxInputData() {

    var data = { };

    var inputs = $('#modalBoxForm input');

    for( var i = 0, ii = inputs.length; i < ii; i++ ) {
        var el = $(inputs[i]);
        data[ el.attr('name') ] = el.val();
    }

    return data;
}
